﻿using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Text;

namespace ST.Common.Util
{
	public class EnumHelper
	{
		/// <summary>
		/// 生成下拉列表
		/// </summary>
		/// <param name="enumType"></param>
		/// <param name="defaultText"></param>
		/// <param name="defaultValue"></param>
		/// <param name="selectedValue"></param>
		/// <returns></returns>
		public static SelectList GetSelectList(Type enumType, string defaultText, string defaultValue, string selectedValue)
		{

			List<SelectListItem> lstItems = new List<SelectListItem>();
			if (!string.IsNullOrEmpty(defaultText))
			{
				lstItems.Add(new SelectListItem() { Text = defaultText, Value = defaultValue });
			}
			Array values = Enum.GetValues(enumType);
			foreach (var v in values)
			{
				string n = Enum.GetName(enumType, v);
				lstItems.Add(new SelectListItem() { Text = n, Value = ((int)v).ToString() });
			}

			return new SelectList(lstItems, "Value", "Text", selectedValue);
		}

		public static SelectList GetSelectList(Type enumType, string defaultText = "请选择")
		{
			return GetSelectList(enumType, defaultText, "", "");
		}
	}
}